package com.vca.admin.controller;

import com.vca.common.page.CommonPage;
import com.vca.common.request.CourseAddRequest;
import com.vca.common.request.PageParamRequest;
import com.vca.common.request.AdminCommonRequest;
import com.vca.common.request.TalkAddRequest;
import com.vca.common.response.*;
import com.vca.common.result.CommonResult;
import com.vca.service.service.TalkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程控制器
 *
 * @author Li
 * @date 2022/11/14   09:56
 */
@Slf4j
@RestController("TalkController")
@RequestMapping("api/admin/talk")
@Api(tags = "讲座控制器")
public class TalkController {

    @Autowired
    private TalkService talkService;

    /**
     * 讲座管理
     * @param request
     * @param pageParamRequest
     * @author Li
     * @date 2022/11/14 10:01
     * @return
     */
    @PreAuthorize("hasAuthority('admin:talk:list')")
    @ApiOperation("讲座管理")
    @GetMapping("/list")
    public CommonResult<CommonPage<TalkAdminListResponse>> getList(@ModelAttribute @Validated AdminCommonRequest request, @ModelAttribute PageParamRequest pageParamRequest){
        CommonPage commonPage = CommonPage.restPage(talkService.getAdminList(request,pageParamRequest));
        return CommonResult.success(commonPage);
    }

    /**
     * 修改排序
     * @param talkId 讲座id
     * @param sort 排序
     * @author Li
     * @date    2022/11/18 15:16
     * @return
     */
    @PreAuthorize("hasAuthority('admin:talk:update:sort')")
    @ApiOperation("修改排序")
    @GetMapping("/updateSort")
    public CommonResult updateSort(Integer talkId,Integer sort){
        return talkService.updateSort(talkId,sort)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 修改限量
     * @param talkId 讲座id
     * @param seatCount 限量
     * @author Li
     * @date    2022/11/18 15:16
     * @return
     */
    @PreAuthorize("hasAuthority('admin:talk:update:quota')")
    @ApiOperation("修改席位")
    @GetMapping("/updateSeatCount")
    public CommonResult updateSeatCount(Integer talkId,Integer seatCount){
        return talkService.updateSeatCount(talkId,seatCount)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 上架讲座
     * @param talkId 讲座id
     * @author Li
     * @date    2022/11/18 15:16
     * @return
     */
    @PreAuthorize("hasAuthority('admin:talk:shell:on')")
    @ApiOperation("上架讲座")
    @GetMapping("/courseOnShell")
    public CommonResult courseOnShell(Integer talkId){
        return talkService.courseOnShell(talkId)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 下架讲座
     * @param talkId 讲座id
     * @author Li
     * @date    2022/11/18 15:16
     * @return
     */
    @PreAuthorize("hasAuthority('admin:talk:shell:off')")
    @ApiOperation("下架讲座")
    @GetMapping("/courseOffShell")
    public CommonResult courseOffShell(Integer talkId){
        return talkService.courseOffShell(talkId)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 添加讲座
     * @param request
     * @author Li
     * @date 2022/11/18 15:38
     * @return
     */
    @PreAuthorize("hasAuthority('admin:talk:add')")
    @ApiOperation("添加讲座")
    @PostMapping("/add")
    public CommonResult addTalk(@RequestBody TalkAddRequest request){
        return talkService.addTalk(request)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 根据id获取讲座信息
     * @param talkId
     * @author Li
     * @date    2022/11/22 10:25
     * @return
     */
    @PreAuthorize("hasAuthority('admin:talk:get')")
    @ApiOperation("根据id获取讲座信息")
    @GetMapping("/getTalk")
    public CommonResult<TalkAdminResponse> getTalk(Long talkId){
        return CommonResult.success(talkService.getTalk(talkId));
    }

    /**
     * @Description:修改讲座
     * @Author: Li
     * @Date: 2022/11/22 14:56
     */
    @PreAuthorize("hasAuthority('admin:talk:update')")
    @ApiOperation(value = "修改讲座")
    @PostMapping("/update")
    public CommonResult<String> update(@RequestBody TalkAddRequest request) {
        return talkService.updateTalk(request)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 获得讲座头部数据
     * @author Li
     * @date 2022/11/23 09:16
     * @return
     */
    @ApiOperation(value = "获得讲座头部数据")
    @GetMapping("/header")
    public CommonResult<List<AdminHeaderResponse>> getHeader() {
        return CommonResult.success(talkService.getHeader());
    }

    /**
     * 删除讲座
     * @author Li
     * @date 2022/11/23 09:16
     * @return
     */
    @PreAuthorize("hasAuthority('admin:talk:delete')")
    @ApiOperation(value = "删除讲座")
    @GetMapping("/delete")
    public CommonResult delete(Long talkId){
        return talkService.deleteTalk(talkId)?CommonResult.success():CommonResult.failed();
    }

}
